package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Disease;
import com.foreknow.entity.Disecategory;
import com.foreknow.service.impl.DiseaseServiceImpl;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/disease")
public class DiseaseController {
    @Autowired
    private DiseaseServiceImpl diseaseService;


    @RequestMapping(value = "/findDiseaseByPage", method = RequestMethod.POST)
    public String findUserByPage(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        System.out.println(para);
        int id = (int) para.get("id");
        int pageNum = (int) para.get("pagenum");
        int pageSize = (int) para.get("pagesize");
        PageHelper.startPage(pageNum, pageSize);
        List<Disease> diseases = diseaseService.selectOneList(id);
        HashMap<String, Object> hs = new HashMap<>();
        if (diseases != null) {
            //PageInfo是PageHelper中内置的分页信息类
            PageInfo<Disecategory> info = new PageInfo(diseases);
            hs.put("data", info.getList());
            hs.put("meta", new Constance("msg", 200));
            hs.put("total", info.getTotal());
        } else {
            hs.put("meta", new Constance("获取疾病列表失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addDisease", method = RequestMethod.POST)
    public String addUser(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Disease d = new Disease();
        String DiseaseCode = (String) para.get("diseasecode");
        String DiseaseName = (String) para.get("diseasename");
        String DiseaseICD = (String) para.get("diseaseicd");
        String DiseCategoryID = (String) para.get("disecategoryid");

        d.setDiseasecode(DiseaseCode);
        d.setDiseasename(DiseaseName);
        d.setDiseaseicd(DiseaseICD);
        d.setDisecategoryid(Integer.parseInt(DiseCategoryID));

        int isRight = diseaseService.addDisease(d);
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        }else {
            hs.put("meta", new Constance("新增疾病信息失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteDisease", method = RequestMethod.DELETE)
    private String delete(@RequestParam Map<String, Object> para) throws JsonProcessingException {
        String id = (String) para.get("id");
        int isRight = diseaseService.deleteById(Integer.parseInt(id));
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("删除失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectDiseaseOne", method = RequestMethod.GET)
    public String selectOne(@RequestParam Map<String, Object> para) throws JsonProcessingException {
        String id = (String) para.get("id");
        Disease d = diseaseService.queryOne(Integer.parseInt(id));
        HashMap<String, Object> hs = new HashMap<>();
        if (d != null) {
            hs.put("meta", new Constance("msg", 200));
            hs.put("data", d);
        } else {
            hs.put("meta", new Constance("查询失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updateDisease", method = RequestMethod.POST)
    public String update(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Disease d = new Disease();
        int id = (int) para.get("id");
        String DiseaseCode = (String) para.get("diseasecode");
        String DiseaseName = (String) para.get("diseasename");
        String DiseaseICD = (String) para.get("diseaseicd");
        Integer DiseCategoryID = (Integer) para.get("disecategoryid");
        d.setId(id);
        d.setDiseasecode(DiseaseCode);
        d.setDiseasename(DiseaseName);
        d.setDiseaseicd(DiseaseICD);
        d.setDisecategoryid(DiseCategoryID);
        int isRight = diseaseService.update(d);
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("修改疾病信息失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findAllDiseaseByPage", method = RequestMethod.POST)
    public String findAllDiseaseByPage(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        System.out.println(para);
        int pageNum = (int) para.get("pagenum");
        int pageSize = (int) para.get("pagesize");
        PageHelper.startPage(pageNum, pageSize);
        List<Disease> disecategories = diseaseService.queryDouble();

        HashMap<String, Object> hs = new HashMap<>();
        if (disecategories != null) {
            //PageInfo是PageHelper中内置的分页信息类
            PageInfo<Disecategory> info = new PageInfo(disecategories);
            hs.put("data", info.getList());
            hs.put("meta", new Constance("msg", 200));
            hs.put("total", info.getTotal());
        } else {
            hs.put("meta", new Constance("获取用户列表失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

}